package com.example.jussi.recyclerviewdemo;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

/**
 * Created by admin on 2018/11/30.
 */

public class WeathyAdapter extends RecyclerView.Adapter<WeathyAdapter.ViewHolder> {

    private List<Weathy> myWeathyList;

    static class ViewHolder extends RecyclerView.ViewHolder{
        View weathyView;
        ImageView weathyImage;
        TextView weathyName;

        public ViewHolder(View itemView) {
            super(itemView);
            weathyView = itemView;
            weathyImage = itemView.findViewById(R.id.weathy_image);
            weathyName = itemView.findViewById(R.id.weathy_name);
        }
    }

    public WeathyAdapter(List<Weathy> weathyList) {
        myWeathyList = weathyList;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.weathy_item, parent, false);
        //View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.weathy_item2, parent, false);
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.weathy_item3, parent, false);
        final ViewHolder holder = new ViewHolder(view);
        holder.weathyView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int position = holder.getAdapterPosition();
                Weathy weathy = myWeathyList.get(position);
                Toast.makeText(view.getContext(), "You clicked view "+weathy.getName(), Toast.LENGTH_SHORT).show();
            }
        });
        holder.weathyImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int position = holder.getAdapterPosition();
                Weathy weathy = myWeathyList.get(position);
                Toast.makeText(view.getContext(), "You clicked image "+ weathy.getName(), Toast.LENGTH_SHORT).show();
            }
        });
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Weathy weathy = myWeathyList.get(position);
        holder.weathyImage.setImageResource(weathy.getImageId());
        holder.weathyName.setText(weathy.getName());
    }

    @Override
    public int getItemCount() {
        return myWeathyList.size();
    }

}
